﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TouristAgencyDataLayer;

namespace TouristAgencyWinApp
{
    public partial class MeanForm : Form
    {
        private MeansOfTransport selectedMean;

        public MeanForm()
        {
            InitializeComponent();
            WinLoad();
        }

        private void WinLoad()
        {
            meansOfTransportBindingSource.DataSource = MeansOfTransports.GetMeansOfTransport();
        }
        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (selectedMean != null && !string.IsNullOrEmpty(this.meanTxt.Text))
            {
                MeansOfTransports.UpdateMeanOfTransport(selectedMean.MeanOfTransportId, this.meanTxt.Text);
                MessageBox.Show("Changes have been saved");
            }
            else if (!string.IsNullOrEmpty(this.meanTxt.Text))
            {
                MeansOfTransports.CreateHotelMeanOfTransport(this.meanTxt.Text);
                MessageBox.Show("New Mean of Transport has been added");
            }
            WinLoad();
        }

        private void meanDG_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != this.meanDG.Rows.Count - 1)
            {
                selectedMean = MeansOfTransports.GetMeanOfTransportById(((MeansOfTransport)this.meanDG.Rows[e.RowIndex].DataBoundItem).MeanOfTransportId);
                this.meanTxt.Text = selectedMean.MeanOFTransport;
            }
            else
            {
                this.meanTxt.Text = "";
                selectedMean = null;
            }
        }

        private void delBtn_Click(object sender, EventArgs e)
        {
            if (selectedMean != null)
            {
                MeansOfTransports.DeleteMeanOfTransport(selectedMean.MeanOfTransportId);
                MessageBox.Show("The selected item has been deleted");
            }
            else
            {
                MessageBox.Show("Select some item first.");
            }
            WinLoad();
        }
    }
}
